﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Mailer.Model.Types;
using System.Collections.Generic;
using Mailer.Model;

namespace Mailer.Views
{
	public class ViewHelpers
	{
		public static string GetSmallMailStatusLabelHTML(Mail m)
		{
			/* GoogleChartSharp.BarChart bc = new GoogleChartSharp.BarChart(300, 30, GoogleChartSharp.BarChartOrientation.Horizontal, GoogleChartSharp.BarChartStyle.Stacked);
			return bc.GetUrl();*/

			string ret = "";

			if (m.State == (int)Mailer.Model.MailState.NotSent)
			{
				ret = "Not sent.";


			}
			else if (m.State == (int)Mailer.Model.MailState.Sending)
			{
				//	ret = "<div class=\"progressbar\"><div class=\"bar\" style=\"width: <%= m.PercentDone %>%;\"></div><span class=\"title\">Sending " + m.PercentDone + "% (" + m.NumSentTotal + "/" + m.NumTotal + ")</span></div>";
				ret = "Sending " + m.NumSentTotal + " of " + m.NumTotal + ". (" + m.PercentDone + "%)";
			}
			else if (m.State == (int)Mailer.Model.MailState.Sent)
			{
				ret = "Sent. " + m.NumRead + " read of " + m.NumSentSuccessfully + ", " + m.NumSendAndFailed + " failed.";

			}
			else if (m.State == (int)Mailer.Model.MailState.Archived)
			{
				ret = "Archived. (" + m.NumRead + " of " + m.NumSentSuccessfully + " read)";
			}
			return ret;
		}

		public static string GetSmallMailStatusGraphHTML(Mail m)
		{
			string ret = "";

			if (m.State == (int)MailState.Sending)
			{
				GoogleChartSharp.PieChart bc = new GoogleChartSharp.PieChart(50, 50, GoogleChartSharp.PieChartType.TwoD);// GoogleChartSharp.BarChartOrientation., GoogleChartSharp.BarChartOrientation.Horizontal, GoogleChartSharp.BarChartStyle.Stacked);
				bc.SetDatasetColors(new string[] { SentColor, NoColor });
				bc.SetData(new int[] { m.PercentDone, 100 - m.PercentDone });
				ret = "<img src=\"" + bc.GetUrl() + "\" />";
			}
			else if (m.State == (int)MailState.Sent ||
				m.State == (int)MailState.Archived)
			{
				GoogleChartSharp.PieChart bc = new GoogleChartSharp.PieChart(50, 50, GoogleChartSharp.PieChartType.TwoD);// GoogleChartSharp.BarChartOrientation., GoogleChartSharp.BarChartOrientation.Horizontal, GoogleChartSharp.BarChartStyle.Stacked);

				//	List<int[]> values = new List<int[]>();
				//	values.Add( new int[] { 6 } );
				//	values.Add( new int[] { 3 } );
				//	values.Add( new int[] { 6, 3, 1 } );
				//	bc.SetData(values);
				bc.SetDatasetColors(new string[] { ReadColor, SentColor, FailColor });
				//	bc.SetPieChartLabels(new string[] { "Unread (" + m.NumSentButNotRead + ")", "Read (" + m.NumRead + ")", "Failed (" + m.NumSendAndFailed + ")" });

				if (m.NumSentTotal > 0)
				{
					int rpct = (100 * m.NumRead) / m.NumSentTotal;
					int sbnrpct = (100 * m.NumSentButNotRead) / m.NumSentTotal;
					int safpct = (100 * m.NumSendAndFailed) / m.NumSentTotal;

					bc.SetData(new int[] { rpct, sbnrpct, safpct });
				}
				else
				{
					bc.SetData(new int[] { 0, 0, 0 });
				}

				// bc.SetData(new int[] { m.NumRead, m.NumSentButNotRead, m.NumSendAndFailed });

				ret = "<img src=\"" + bc.GetUrl() + "\" />";
			}

			return ret;
		}


		public static string GetBigMailStatusGraphHTML(Mail m)
		{
			string ret = "";
			if (m.State == (int)MailState.Sent ||
				m.State == (int)MailState.Archived)
			{
				GoogleChartSharp.PieChart bc = new GoogleChartSharp.PieChart(160, 140, GoogleChartSharp.PieChartType.TwoD);// GoogleChartSharp.BarChartOrientation., GoogleChartSharp.BarChartOrientation.Horizontal, GoogleChartSharp.BarChartStyle.Stacked);

				//	List<int[]> values = new List<int[]>();
				//	values.Add( new int[] { 6 } );
				//	values.Add( new int[] { 3 } );
				//	values.Add( new int[] { 6, 3, 1 } );
				//	bc.SetData(values);
				bc.SetDatasetColors(new string[] { ReadColor, SentColor, FailColor });
				// bc.SetPieChartLabels(new string[] { "Read (" + m.NumRead + ")", "Unread (" + m.NumSentButNotRead + ")", "Failed (" + m.NumSendAndFailed + ")" });
				// bc.SetPieChartLabels(new string[] { "Read", "Unread", "Failed" });
				// bc.SetLegend(new string[] { "Read (" + m.NumRead + ")", "Unread (" + m.NumSentButNotRead + ")", "Failed (" + m.NumSendAndFailed + ")" });


				int rpct = (100 * m.NumRead) / m.NumSentTotal;
				int sbnrpct = (100 * m.NumSentButNotRead) / m.NumSentTotal;
				int safpct = (100 * m.NumSendAndFailed) / m.NumSentTotal;

				bc.SetData(new int[] { rpct, sbnrpct, safpct });

				ret = "<img src=\"" + bc.GetUrl() + "\" />";

			}

			return ret;
		}

		public static string GetBigMailStatusLegendHTML(Mail m)
		{
			string ret = "";
			if (m.State == (int)MailState.Sent ||
				m.State == (int)MailState.Archived)
			{ 
				ret = "<ul class=\"legend\">"
					+ "<li><span style=\"background-color:#" + SentColor + "\">&nbsp;</span> " + m.NumSentTotal + " Sent in total.</li>"
					+ "<li><span style=\"background-color:#" + ReadColor + "\">&nbsp;</span> " + m.NumRead + " Read by recipient.</li>"
					+ "<li><span style=\"background-color:#" + FailColor + "\">&nbsp;</span> " + m.NumSendAndFailed + " Failed to send.</li>"
					+ "</ul>";

			}

			return ret;
		}

		const string SentColor = "33cc44";
		const string ReadColor = "eecc33";
		const string FailColor = "cc5533";
		const string NoColor = "eeeeee";

	}
}
